package mylooks.greenteam.Registration;

import mylooks.greenteam.DatabaseHelper;
import mylooks.greenteam.MainActivity;
import mylooks.greenteam.R;
import android.R.string;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class RegisterActivity extends FragmentActivity implements View.OnClickListener
{
	ViewPager register_pager;
	RegistrationAdapter adapter;
	
	Button btnNext, btnPrevious, btnFinish;
	
	DatabaseHelper dbH;
	
	@Override
	protected void onCreate(Bundle arg0) 
	{
		super.onCreate(arg0);
		setContentView(R.layout.registration_main);
		
		dbH = new DatabaseHelper(this);
		
		if(dbH.isUserAlreadyRegisterd())
		{
			startActivity(new Intent(RegisterActivity.this, MainActivity.class));
			finish();
		}
		
		register_pager = (ViewPager)findViewById(R.id.registration_pager);
		adapter = new RegistrationAdapter(getSupportFragmentManager());
		register_pager.setAdapter(adapter);
		
		btnNext = (Button)findViewById(R.id.btnnext);
		btnNext.setOnClickListener(this);
		btnPrevious = (Button)findViewById(R.id.btnprevious);
		btnPrevious.setOnClickListener(this);
		btnFinish = (Button)findViewById(R.id.btnfinish);
		btnFinish.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) 
	{
		switch(v.getId())
		{
			case R.id.btnnext :
				
				if(register_pager.getCurrentItem() + 1 < adapter.getCount())
				{	
					register_pager.setCurrentItem(register_pager.getCurrentItem() + 1, true);
				
					if(register_pager.getCurrentItem() + 1 == adapter.getCount())
					{
						btnNext.setVisibility(View.GONE);
						btnFinish.setVisibility(View.VISIBLE);
					}
					
					btnPrevious.setVisibility(View.VISIBLE);
				}
				
				break;
				
			case R.id.btnprevious :
				
				if(register_pager.getCurrentItem() > 0)
				{
					register_pager.setCurrentItem(register_pager.getCurrentItem() - 1, true);
					
					if(register_pager.getCurrentItem() == 0)
						btnPrevious.setVisibility(View.GONE);
				}
				
				btnFinish.setVisibility(View.GONE);
				btnNext.setVisibility(View.VISIBLE);
				
				break;
				
			case R.id.btnfinish : 
				
				if(dbH.InsertUser(adapter.getUser()))
				{
					if(String.valueOf(adapter.getUser().getBirthdate()) != "")
					{
						Context context = getApplicationContext();
						int duration = Toast.LENGTH_SHORT;
						Toast toast = Toast.makeText(context, "Sta inserendo", duration);
						toast.show();
					}
					startActivity(new Intent(RegisterActivity.this, MainActivity.class));
					finish();
				}
				else
				{
					
				}
				
				break;
		}		
	}	
}
